package com.echo.im.platform.manager;

import com.echo.im.client.facade.IMClient;
import com.echo.im.common.model.message.IMPrivateMessage;
import com.echo.im.mq.event.model.PrivateStoreMessageEvent;
import com.echo.im.mq.event.producer.EventPublisher;
import com.echo.im.platform.core.context.SessionContext;
import com.echo.im.platform.core.context.UserSession;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author: echo
 * @time: 2024-08-17
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class PrivateMessageManager {

    private final IMClient imClient;
    private final EventPublisher eventPublisher;
    private final PrivateStoreMessageEvent privateStoreMessageEvent;

    public void sendAndSave(IMPrivateMessage imPrivateMessage,Boolean isSave) {
        UserSession session = SessionContext.getSession();
        imClient.sendMessage(imPrivateMessage);
        if (isSave){
            eventPublisher.publish(privateStoreMessageEvent.topic(), privateStoreMessageEvent.buildEventMessage(imPrivateMessage.getData()));
        }
        // 持久化消息
        log.info("发送私聊消息，发送id:{},接收id:{}，内容:{}", session.getUserId(), imPrivateMessage.getFirstRecvId(), imPrivateMessage.getData());
    }

    public void sendAndSave(IMPrivateMessage imPrivateMessage) {
        sendAndSave(imPrivateMessage,true);
    }

}
